package com.ebspp.server;

import java.util.List;
import java.util.Map;

import org.apache.thrift.TException;

import com.ebspp.global.General;
import com.ebspp.server.resource.ClientResManager;
import com.ebspp.server.resource.ResourceManager;
import com.ebspp.thrift.DataMsg;
import com.ebspp.thrift.DataRead;
import com.ebspp.thrift.Identity;
import com.ebspp.thrift.ResourceType;

/**
 * Handle read requests.
 * The refer functionality is under construction
 * @author x23pan
 *
 */
public class ReadHandler implements DataRead.Iface
{
	@Override
	public List<DataMsg> readEBSPP(List<DataMsg> keys, ResourceType type,
			Identity id) throws TException {
		// TODO Auto-generated method stub
		
		ClientResManager resManager = ResourceManager.getResManager(id);
		if(resManager == null)
		{
			System.err.println("Resource unprovisioned");
			return keys;
		}
		
		return resManager.read(keys, type);
	}

	@Override
	public String provision(Map<ResourceType, Integer> resource, Identity id) throws TException 
	{
		if(resource == null || id == null)
			return null;
		
		System.out.println("Receive read provision from " + id.getKey());
		boolean succ =  ResourceManager.updateResource(resource, id);
		if(succ)
			return General.getServerName();
		else
			//todo
			//currently, I have not implemented strategy for returning alternative server
			return General.getServerName();
	}
}
